package grid
{
	
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.containers.TitleWindow;
	import mx.controls.Alert;
	import mx.core.Application;
	import mx.managers.PopUpManager;
	
	

	// Advanced Targeting And Analysis Module
	public class ATAAM
	{	import mx.core.mx_internal;
		use namespace mx_internal;
		public var tw = new TitleWindow();
		public function ATAAM()
		{
			//nothing much...
			
		}
		public var previousHits = new Array();
		public var previousMisses = new Array();
		public var humanArray = new Array();
		public var previousAIHits = new Array();
		public var totalAIHits = new Array();
		public var previousAIMisses = new Array();
		public var topology = new Topology();
		public var internalCounter = 0;
		private var newHitOptions = new Array();
		
		private var lastHit = {HIT:undefined};
		private var lastHitAI = {HIT:undefined};
		private var set=0;
		var i=0;
		public function Lock_n_Load(coords,unitsArr,AI=false,wasHit=false)
		{
			var arrHit = new Array();
			var arrMiss = new Array();
			if(AI){arrHit=previousAIHits;arrMiss = previousAIMisses}
			else {arrHit=previousHits;arrMiss = previousMisses;}
			var isHit = false;
			if(wasHit) i=i+1;
			if(i>12){i=0;lastHit={HIT:isHit,X:coords.X,Y:coords.Y};}
			if(arrHit.indexOf(String(coords.X)+String(coords.Y))==-1&&arrMiss.indexOf(String(coords.X)+String(coords.Y))==-1)
			{
				for each(var o in unitsArr)
				{
					for each(var p in o)
					{
						if(p.X==coords.X&&p.Y==coords.Y)
						{
							isHit = true;AI==true?previousAIHits.push(String(coords.X)+String(coords.Y)):previousHits.push(String(coords.X)+String(coords.Y));
							
						}
						else
						{}
					}
				}
				if(isHit==false)
				{
					AI==true?previousAIMisses.push(String(coords.X)+String(coords.Y)):previousMisses.push(String(coords.X)+String(coords.Y));
					if(AI==false)lastHit={HIT:isHit,X:coords.X,Y:coords.Y}
				}
				else
				{lastHit = {HIT:isHit,X:coords.X,Y:coords.Y,Counter:i};}
				if(AI)return isHit;
				else
					return lastHit;
			}
			else
			{lastHit={HIT:undefined,Counter:0};return "undefined";}
		}
		
		// LOCK AND LOAD FOR AI.... need to simplyfy this
		public function Search_n_Destroy(coords,unitsArr,AI=false,wasHit=false)
		{
			var arrHit = new Array();
			var arrMiss = new Array();
			if(AI){arrHit=previousAIHits;arrMiss = previousAIMisses}
			else {arrHit=previousHits;arrMiss = previousMisses;}
			var isHit = false;
			if(wasHit) i=i+1;
			if(i>12){i=0;lastHitAI={HIT:isHit,X:coords.X,Y:coords.Y,Counter:i};set=0;}
			if(arrHit.indexOf(String(coords.X)+String(coords.Y))==-1&&arrMiss.indexOf(String(coords.X)+String(coords.Y))==-1)
			{
				for each(var o in unitsArr)
				{
					for each(var p in o)
					{
						if(p.X==coords.X&&p.Y==coords.Y)
						{
							isHit = true;AI==true?previousAIHits.push(String(coords.X)+String(coords.Y)):previousHits.push(String(coords.X)+String(coords.Y));
							totalAIHits.push({X:coords.X,Y:coords.Y});
						}
						else
						{}
					}
				}
				if(isHit==false)
				{
					AI==true?previousAIMisses.push(String(coords.X)+String(coords.Y)):previousMisses.push(String(coords.X)+String(coords.Y));
					if(AI==false)lastHitAI={HIT:isHit,X:coords.X,Y:coords.Y}
				}
				else
				{lastHitAI = {HIT:isHit,X:coords.X,Y:coords.Y,Counter:i==0?i+1:i};}
				if(AI)return isHit;
				else
					return lastHitAI;
			}
			else
			{lastHitAI={HIT:undefined,Counter:0};return "undefined";}
		}
		

		var t; 
		// HIT BY TURNS
		public function AITargeting(humanArray)
		{	
			var result = endComputing(humanArray);
			return result;
		}
		
		/**
		 * 
		 * @param evt When timer ends to emulate thinking
		 * @return 
		 * 
		 */
		public function endComputing(arr)
		{
			
			if(lastHitAI.HIT==false||lastHitAI.HIT==undefined)
			{
				var posX = Math.round(Math.random()*10);
				var posY = Math.round(Math.random()*10);
				while((previousAIHits.indexOf(String(posX)+String(posY))>-1)||
					(previousAIMisses.indexOf(String(posX)+String(posY))>-1)||
					((posX>10||posX<1)||(posY>10||posY<1)))
				{
					posX = Math.round(Math.random()*10);
					posY = Math.round(Math.random()*10);
				}
				var result = Search_n_Destroy({X:posX,Y:posY},arr,true);
				
				if(result=="undefined") endComputing(arr);
				else if(result==false) {return {HIT:false,X:posX,Y:posY};}
				else 
				{previousAIHits.push(String(posX)+String(posY));return {HIT:true,X:posX,Y:posY}}
					
			}
			else if(lastHitAI.HIT==true)
			{
				// if has last hit then it continues with the most possible in the vicinity, else if it has counter==0 it means that
				// more than 12 hits have been made which are the max possible points around the 1st hit, then it returns to full random
				var posX = lastHitAI.X;
				var posY = lastHitAI.Y;
				var whileCounter = 0;
				if(lastHitAI.Counter==1)newHitOptions = topology.getMaxOptions({X:posX,Y:posY});
				var pos;
				
				if(set>=0&&set<4) pos =  Math.round(randomRange(0,3));
				else if(set>=4&&set<8) pos =  Math.round(randomRange(4,7));
				else if(set>=8) pos =  Math.round(randomRange(8,11));
				while(pos>=11)
				{
							if(set>=0&&set<4)pos = Math.round(randomRange(0,3));
							else if(set>=4&&set<8) pos =  Math.round(randomRange(4,7));
							else if(set>=8) pos =  Math.round(randomRange(8,11));
					
				}
				var choise = newHitOptions[pos];
				posX = lastHitAI.Counter>0?choise.X:Math.round(Math.random()*10);
				posY = lastHitAI.Counter>0?choise.Y:Math.round(Math.random()*10);
				while(((posX<1||posY>10)||(posX>10||posY<1))||
					(previousAIHits.indexOf(String(posX)+String(posY))>-1)||
					previousAIMisses.indexOf(String(posX)+String(posY))>-1)
				{
					var rand;
					
					if(set>=0&&set<4)rand =  Math.round(randomRange(0,3));
					else if(set>=4&&set<8) rand =  Math.round(randomRange(4,7));
					else if(set>=8) rand =  Math.round(randomRange(8,11));
					
					whileCounter++;
					choise = newHitOptions[rand];
					posX = whileCounter>=30?Math.round(Math.random()*10):choise.X;
					posY = whileCounter>=30?Math.round(Math.random()*10):choise.Y;
				}
				var result = Search_n_Destroy({X:posX,Y:posY},arr,true,true);
				set++;
				if(result=="undefined") endComputing(arr);
				else if(result==false) {return {HIT:false,X:posX,Y:posY};}
				else 
				{
					previousAIHits.push(String(posX)+String(posY));
					internalCounter++;
					
					if(internalCounter>=5||checkIfDestroyed({X:posX,Y:posY})){lastHit={HIT:true,X:posX,Y:posY,Counter:12};internalCounter=0;set=0;}
					return {HIT:true,X:posX,Y:posY}
				}
				
			
			}
		
		}
		
		public function fillArr(arr)
		{
			var c = 0;
			for each(var o in arr)
			{
				DestroyedUnits[c]= [];
				for each(var p in o)
				{
					DestroyedUnits[c].push({X:p.X,Y:p.Y,unit:p.unit});
				}
				c++;
			}
		}
		
		public var DestroyedUnits = new Array();
		public function checkIfDestroyed(num)
		{
			var i=0;
			for each(var o in humanArray)
			{ 
				var j=0;
				var done=0;
				var destroyed = false;
				var arr = new Array();
				for each(var point in o)
				{
					for each(var h in totalAIHits)
					{
						if(point.X==h.X&&point.Y==h.Y)
						{
							if(point.X==num.X&&point.Y==num.Y)
							destroyed=true;
							
							done++;
						}
						else
						{}//DestroyedUnits[i][j] = {X:num.X,Y:num.Y,done:false,unit:point.unit};
					}
					trace(done);
					
					
					j++;	
				}
				if(done==o.length)
					return destroyed
				else 
					return false;
				i++;
			}
		}
		
		function randomRange(minNum:Number=0, maxNum:Number=11):Number
		{
			return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
		}
	}
}